package com.yogee.topseller.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.yogee.core.base.HttpActivity;
import com.yogee.core.http.listener.HttpOnNextListener;
import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.topseller.R;
import com.yogee.topseller.adapter.IntelligentScreeningListRvAdapter;
import com.yogee.topseller.adapter.SearchResultAdapter;
import com.yogee.topseller.bean.InterlligentFilterBean;
import com.yogee.topseller.bean.MySelfInfo;
import com.yogee.topseller.http.HttpManager;
import com.yogee.topseller.layout.TitleLayout;
import com.yogee.topseller.utils.AppUtil;
import com.yogee.topseller.utils.LogUtils;
import com.yogee.topseller.utils.RefreshUtil;
import com.yogee.topseller.view.EmptyRecyclerView;
import com.yogee.topseller.view.MySinaRefreshView;
import com.yogee.topseller.view.RefreshView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 搜索结果页面,进入此页面要传inputText值进来，即前页面的输入内容
 *
 * @author hanyu
 *         created by 2017/7/19 17:46
 */

public class SearchResultActivity extends HttpActivity implements RefreshUtil.OnRefreshListener {

    @BindView(R.id.refreshLayout)
    TwinklingRefreshLayout refreshLayout;

    @BindView(R.id.add_card_titlelayout)
    TitleLayout titlelayout;
    @BindView(R.id.id_empty_view)
    View id_empty_view;
    @BindView(R.id.id_empty_view1)
    View id_empty_view1;
    @BindView(R.id.iv_empty)
    ImageView iv_empty;
    private TextView title;
    private EmptyRecyclerView resultList;
    private SearchResultAdapter mAdapter;

    private String inputText;
    private String total = "0";
    private String count = "10";
    private List<InterlligentFilterBean.ProductListBean> beans;
    private boolean isRefresh = false ;
    
    private int fristTime = 1;
    private String yjjs="",tzcs="",tzqx="",cplx="",fxjg="",diqu="",tzly="";
    private String type="";
    @Override
    protected int getLayoutId() {
        return R.layout.activity_search_result;
    }

    @Override
    protected void initView() {
        beans = new ArrayList<>();
        mAdapter = new SearchResultAdapter(this);
        titlelayout.setActivity(this);
        titlelayout.setTitle("搜索结果");

        resultList = (EmptyRecyclerView) findViewById(R.id.lv_search_result_list);
        resultList.setLayoutManager(new LinearLayoutManager(this));
        resultList.setAdapter(mAdapter);

        MySinaRefreshView headerView = new MySinaRefreshView(this);
        refreshLayout.setOnRefreshListener(new RefreshUtil(this).refreshListenerAdapter());
        refreshLayout.setHeaderView(headerView);
        refreshLayout.setBottomView(new RefreshView(this));
        refreshLayout.setBottomHeight(50);
        if (null != getIntent()) {
            inputText = getIntent().getStringExtra("inputText");

            yjjs = getIntent().getStringExtra("yjjs");
            tzcs = getIntent().getStringExtra("tzcs");
            tzqx = getIntent().getStringExtra("tzqx");
            cplx = getIntent().getStringExtra("cplx");
            fxjg = getIntent().getStringExtra("fxjg");
            diqu = getIntent().getStringExtra("diqu");
            tzly = getIntent().getStringExtra("tzly");
            type = getIntent().getStringExtra("type");
            iv_empty.setImageResource(R.drawable.nodata8);
            LogUtils.e("sfz","type:"+type);
            if(type.equals("1")){
                resultList.setEmptyView(id_empty_view);
            }else{
                resultList.setEmptyView(id_empty_view1);
            }
        }
        loadSearch(total, count);
    }

    /**
     * 加载搜索结果
     */
    private void loadSearch(String total, String count) {
        if(type!=null&&"1".equals(type)){
            HttpManager.getInstance().searchResult1(AppUtil.getUserId(SearchResultActivity.this), yjjs,tzcs,tzqx,cplx,fxjg,diqu,tzly, total, count).compose(this.bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<InterlligentFilterBean>() {
                @Override
                public void onNext(InterlligentFilterBean o) {
                    MySelfInfo.getInstance().setIsPay(o.getIspay());
                    LogUtils.e("sfz","ispay:"+o.getIspay());
                    LogUtils.e("sfz","ispay:"+MySelfInfo.getInstance().getIsPay());
                    loadingFinished();
                    if (null != o.getProductList()){
                        if (isRefresh){
                            refreshLayout.finishRefreshing();
                            beans = o.getProductList();
                            //判断是否有数据+
//                            isShowNoData(o.getProductList());
                        }else{
                            refreshLayout.finishLoadmore();
                            beans.addAll(o.getProductList());
                            if (1 == fristTime){
                                //判断是否有数据
//                                isShowNoData(o.getProductList());
                                fristTime = 0;
                            }
                        }
                        mAdapter.addData(beans);
                    }
                }
            }, this));
        }else{
            HttpManager.getInstance().searchResult(AppUtil.getUserId(SearchResultActivity.this), inputText, total, count).compose(this.bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<InterlligentFilterBean>() {
                @Override
                public void onNext(InterlligentFilterBean o) {
                    loadingFinished();
                    if (null != o.getProductList()){
                        if (isRefresh){
                            refreshLayout.finishRefreshing();
                            beans = o.getProductList();
                            //判断是否有数据
//                            isShowNoData(o.getProductList());
                        }else{
                            refreshLayout.finishLoadmore();
                            beans.addAll(o.getProductList());
                            if (1 == fristTime){
                                //判断是否有数据
//                                isShowNoData(o.getProductList());
                                fristTime = 0;
                            }
                        }
                        mAdapter.addData(beans);
                    }
                }
            }, this));
        }

    }


    private void isShowNoData(List<InterlligentFilterBean.ProductListBean> bean){
//        if (0 == bean.size()){
//            tvNoData.setVisibility(View.VISIBLE);
//            refreshLayout.setVisibility(View.GONE);
//        }else{
//            tvNoData.setVisibility(View.GONE);
//            refreshLayout.setVisibility(View.VISIBLE);
//        }
    }

    @Override
    public void onRefresh() {//刷新
        isRefresh = true;
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                refreshLayout.finishRefreshing();
            }
        }, 1500);
        loadSearch("0", count);
    }

    @Override
    public void onLoad() {//加载
        isRefresh = false;
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                refreshLayout.finishLoadmore();
            }
        }, 1500);
        loadSearch(beans.size() + "", count);
    }
    @OnClick({R.id.tv_miaoshu1})
    public void onClick(View v){
        switch (v.getId()){
            case R.id.tv_miaoshu1:
                startActivity(new Intent().setClass(SearchResultActivity.this,FankuiActivity.class).putExtra("inputText",inputText));
                break;
            default:
                break;
        }
    }

}
